package com.icetec.iceintel.ui.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import java.util.ArrayList;

import com.icetec.R;
import com.icetec.iceintel.database.DevicesListSqlHelper;
import com.icetec.iceintel.framework.AppApplication;
import com.icetec.iceintel.model.Device;
import com.icetec.iceintel.netrequest.UniResAllocator;

/**
 * Created by amo on 2014/12/5.
 */
public class AddgroupAdapter extends BaseAdapter {

    private ArrayList<Device> deviceArrayList=new ArrayList<Device>();
    LayoutInflater layoutInflater;
    DevicesListSqlHelper devicesListSqlHelper;
    Animation animationIn,animationOut;
    int arraySize;



    public AddgroupAdapter(){

        devicesListSqlHelper=new DevicesListSqlHelper();
        this.deviceArrayList.addAll(devicesListSqlHelper.getAllDeviceEceeptGroup());
        layoutInflater=LayoutInflater.from(AppApplication.getShareInstance());
        animationIn=new AlphaAnimation(0.0f,1.0f);
        animationIn.setDuration(200);
        animationOut=new AlphaAnimation(1.0f,0f);
        animationOut.setDuration(200);


    }


    public void updateListViewFromDb(){
        this.deviceArrayList.addAll(devicesListSqlHelper.getAllDevice());
        //从数据库中更新 deviceArrayList
        notifyDataSetChanged();
    }

    public void updateChoosedDevice(ArrayList<Device> deviceArrayList){
        for(Device device : deviceArrayList){
            for(Device d : this.deviceArrayList){
                if(device.getMAC().equals(d.getMAC())){
                    d.setExtra("yes");
                }
            }
        }
    }


    public ArrayList<Device> getDealWithList(){
        ArrayList<Device> vailedList=new ArrayList<Device>();
        for(Device device:deviceArrayList){
            if(device.getExtra()!=null&&device.getExtra().equals("yes")){
                vailedList.add(device);
            }
        }
        return vailedList;
    }



    @Override
    public int getCount() {
        arraySize=deviceArrayList.size();
        return  arraySize;
    }

    @Override
    public Object getItem(int position) {
        return deviceArrayList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }


    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        final LightListHolder lightListHolder;
        final  Device device=(Device)getItem(position);

        if(convertView==null){

            convertView=(View)layoutInflater.inflate(R.layout.component_addgroup_lightitems,null);
            lightListHolder=new LightListHolder();
            lightListHolder.lightName=(TextView)convertView.findViewById(R.id.lightlist_item_group_tv);
            lightListHolder.lightIcon=(ImageView)convertView.findViewById(R.id.scheme_devicelist_icon_iv);
            lightListHolder.lightSelector=(CheckBox)convertView.findViewById(R.id.lightlist_item_group_checkbox);
            lightListHolder.itemLayout=(RelativeLayout)convertView.findViewById(R.id.lightlist_item_group_rely);

            convertView.setTag(lightListHolder);
        }
        else
        {
            lightListHolder=(LightListHolder)convertView.getTag();
        }

        lightListHolder.lightSelector.setClickable(false);

        if(device.getExtra()==null||device.getExtra().equals("no")){

          lightListHolder.lightSelector.setChecked(false);

        }

        else{

            lightListHolder.lightSelector.setChecked(true);

        }


        lightListHolder.itemLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(device.getExtra()==null||device.getExtra().equals("no")){

                      device.setExtra("yes");
                    lightListHolder.lightSelector.setChecked(true);
                }

                else{
                    lightListHolder.lightSelector.setChecked(false);
                    device.setExtra("no");

                }
            }
        });

        if(device.getOnoff()!=null&&device.getOnoff().equals("on")){

            lightListHolder.lightIcon.setImageDrawable(UniResAllocator.getResIDbyType(device.getType(), true));

        }

        else {

            lightListHolder.lightIcon.setImageDrawable(UniResAllocator.getResIDbyType(device.getType(),false));
        }

        if(device.getName()!=null) {
            lightListHolder.lightName.setText(device.getName());

        }


        else {
            lightListHolder.lightName.setText(R.string.unnamed_light);
        }

        return  convertView;
    }


    public  class LightListHolder{
        ImageView lightIcon;
        CheckBox lightSelector;
        TextView lightName;
        RelativeLayout itemLayout;
    }


}
